package com.fzsf.drawingboard.adapter

import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.View.LAYER_TYPE_SOFTWARE
import android.view.ViewGroup
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.fzsf.drawingboard.utils.ScreenUtils
import com.fzsf.drawingboard.view.MultiWritingBoard
import com.fzsf.drawingboard.R

/**
 * Created by Taijl on 2018/3/26.
 */
class MultiWritingAdapter(var resultList: MutableList<String>) : RecyclerView.Adapter<MultiWritingAdapter.MultiWritingViewHolder>() {
    var multiWritingBoard: MultiWritingBoard? = null

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): MultiWritingViewHolder {
        return if (viewType == 1) {
            val writingBoard: View = MultiWritingBoard(parent?.context).apply {
                this.layoutParams = RecyclerView.LayoutParams(ScreenUtils.Dp2Px(100F), ScreenUtils.Dp2Px(100F))
            }
            MultiWritingViewHolder(writingBoard)
        } else {
            val multiWritingBoard: View = ImageView(parent?.context).apply {
                this.layoutParams = RecyclerView.LayoutParams(ScreenUtils.Dp2Px(100F), ScreenUtils.Dp2Px(100F))
                this.scaleType = ImageView.ScaleType.CENTER_INSIDE
                this.setLayerType(LAYER_TYPE_SOFTWARE,null)
                this.setBackgroundResource(R.drawable.drawable_matts)
            }
            MultiWritingViewHolder(multiWritingBoard)
        }
    }

    override fun getItemCount(): Int {
        return resultList.size + 1
    }

    override fun onBindViewHolder(holder: MultiWritingViewHolder?, position: Int) {
        if (position < resultList.size) {
            val resultPath = resultList[position]
            if (holder?.itemView is ImageView) {
                Glide.with(holder.itemView.context).load(resultPath).asBitmap().into(holder.itemView as ImageView)
            }
        } else if (position == resultList.size) {
            multiWritingBoard = holder?.itemView as MultiWritingBoard
        }
    }

    override fun getItemViewType(position: Int): Int {
        if (position == resultList.size) {
            return 1
        }
        return super.getItemViewType(position)
    }

    class MultiWritingViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        init {

        }
    }
}